package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.Manager;
import com.example.mybatisplus.mapper.ManagerMapper;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.ManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author lxp
 * @since 2024-07-17
 */
@Service
public class ManagerServiceImpl extends ServiceImpl<ManagerMapper, Manager> implements ManagerService {

    @Override
    public Manager login(Manager manager) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("managerId",manager.getManagerId());
        Manager one = baseMapper.selectOne(wrapper);
        //登录时把登录用户存到session
        if(one!=null){
            //登录成功时把登录用户存到session
            SessionUtils.saveCurrentUser(one);
        }
        return one;
    }

    @Override
    public Manager change(Manager manager) {
        QueryWrapper<Manager> wrapper = new QueryWrapper<>();
        wrapper.eq("managerId",manager.getManagerId());
        Manager one = baseMapper.selectOne(wrapper);
        return one;
    }

    @Override
    public Page<Manager> pageList(Manager manager, PageDTO dto) {
        QueryWrapper<Manager> wrapper =new QueryWrapper<>();
        if(Objects.nonNull(manager.getManagerId())){
            wrapper.eq("managerId",manager.getManagerId());
        }
        Page<Manager> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.selectPage(page,wrapper);
        return page;
    }
}
